#include "stdafx.h"

TFile::TFile()
{
	IsOpened=0;
	file=0;
}
TFile::~TFile()
{
	if(IsOpened)
		fclose(file);
}
TFile::TFile(char *filename, uint Params)
{
	IsOpened=0;
	file=0;
	Open(filename,Params);
}
void TFile::Open(char*filename,uint Params)
{
	if (IsOpened)
		fclose(file);
	file=0;
	char param[4]={'\0','\0','\0','\0'};
	bool readmode=0, translatemode=0;
	if (Params&P_READ)
	{
		param[0]='r';
		readmode=1;
	}
	if ((Params&P_WRITE)&&(!readmode))
	{
		param[0]='w';
		readmode=1;
	}
	if ((Params&P_ADD)&&(!readmode))
	{
		param[0]='a';
		readmode=1;
	}
	if (Params&P_UPDATE)
	{
		param[1]='+';
	}
	if ((Params&P_TEXT)&&(!translatemode))
	{
		param[2]='t';
		translatemode=1;
	}
	if ((Params&P_BINARY)&&(!translatemode))
	{
		param[2]='b';
		translatemode=1;
	}
	file=fopen(filename,param);
	if (file!=NULL)
		IsOpened=1;
}
void TFile::Write(void * buf,uint uiSize, uint uiCount)
{
	fwrite(buf,uiSize,uiCount,file);
}
void TFile::Read(void *buf, uint uiSize, uint uiCount)
{
	fread(buf,uiSize,uiCount,file);
}
int TFile::Seek(int nCount, int StartPoint)
{
	fseek(file,nCount,StartPoint);
	return ftell(file);
}
FILE *TFile::Get()
{
	return file;
}